//
//  TabBar.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/5.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "TabBar.h"

@implementation TabBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"plus_Last"] forState:UIControlStateNormal];
//        btn.backgroundColor = [UIColor redColor];
//        [btn setTitle:@"发布借款" forState:UIControlStateNormal];
        btn.bounds = CGRectMake(0, 0, 64, 64);
        self.centerBtn = btn;
        [self addSubview:btn];
        
        UILabel *borrowLb = [[UILabel alloc] init];
        borrowLb.text = @"发布借款";
        borrowLb.font = [UIFont systemFontOfSize:10];
        borrowLb.textColor = [UIColor grayColor];
        [borrowLb sizeToFit];
        
        CGPoint center = btn.center;
        center.x = kScreenWidth / 2;
        center.y = CGRectGetMaxY(btn.frame) + 0.5 * 10 + 0.5 + 3;
        borrowLb.center = center;
        [self addSubview:borrowLb];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.1);
    int index = 0;
    CGFloat wight = self.bounds.size.width / 5;
    
    for (UIView * sub in self.subviews) {
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            CGFloat tabbarHeight = iPhoneX ? (kTabbarHeight - 34) : kTabbarHeight;
            sub.frame = CGRectMake(index * wight, self.bounds.origin.y, wight, tabbarHeight - 2);
            index ++;
            if (index == 2) {
                index++;
            }
        }
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.isHidden == NO) {
        CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
        if ([self.centerBtn pointInside:newPoint withEvent:event]) {
            return self.centerBtn;
        }else{
            return [super hitTest:point withEvent:event];
        }
    }else{
        return [super hitTest:point withEvent:event];
    }
}

@end
